﻿Imports System.Data
Imports CrystalDecisions.Shared
Imports System.Data.OleDb
Imports System
Imports System.Globalization
Partial Class Interfaz_ModuloPlanificacion_frmConsultas
    Inherits System.Web.UI.Page
    Dim CL As Logica.ModuloPlanificacion.ControladorLogica
    Dim CLAdmin As Logica.ModuloAdministracion.ControladorLogica

    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        CL = Logica.ModuloPlanificacion.ControladorLogica.getInstance
        CLAdmin = Logica.ModuloAdministracion.ControladorLogica.getInstance

        If CL.visibilidadesFormsInternos("Planificacion", Session("user").ToString) = False Then
            Me.Mensaje("Usted NO cuenta con los permisos necesarios para ingresar a esta sección. Para solucionar este problema favor comunicarse con el administrador de sistemas")
            Response.Redirect("Index.aspx")
        End If

        If Not Page.IsPostBack Then
            Try
                llenarCBot()
                'llenarCBPlanificacion()
                'llenarCBMarcaComponente()
            Catch
            End Try
            fillRadioButtonCriterio()
        Else
        End If

    End Sub

    Protected Sub BtnAddCriterio_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnAddCriterio.Click
        Dim it As New ListItem
        it.Text = Me.TextBox2.Text
        it.Value = Me.TextBox1.Text
        Try
            CL.IngresarCriterioBusqueda(it)
            Me.RblCriterioDeBusqueda.Items.Add(it)
        Catch ex As Exception
            Me.Mensaje("No se pudieron Ingresar Los Datos, Consulte con su administrador de sistema.")
        End Try

    End Sub

    Private Sub fillRadioButtonCriterio()
        Dim cconsulta As ArrayList = CL.ObtenerEstaciones("", 1)
        For i As Integer = 0 To cconsulta.Count - 1
            Dim rb As ListItem = New ListItem("Planificacion por " & DirectCast(cconsulta.Item(i), Estacion).Estacion, DirectCast(cconsulta.Item(i), Estacion).ID)
            Me.RblCriterioDeBusqueda.Items.Add(rb)
        Next
    End Sub

#Region "Utilidades"

    Public Sub Mensaje(ByVal msg As String)
        Dim alertScript As String = "<script language=JavaScript runat=server>"
        alertScript &= "alert('" & msg & "');"
        alertScript &= "</script>"
        ClientScript.RegisterClientScriptBlock(Page.GetType(), "clientScrip", alertScript)
    End Sub

    Public Sub llenarCBot()

        Dim ot As ArrayList = CL.ObtenerOT("", 1)

        If ot.Count > 0 Then
            For i As Integer = 0 To ot.Count - 1
                Dim item As New ListItem
                Dim presupuesto As ArrayList = Me.CLAdmin.ObtenerPresupuestoCriterio(DirectCast(ot.Item(i), OrdenTrabajo).FkPresupuesto)
                item.Value = DirectCast(ot.Item(i), OrdenTrabajo).Id
                item.Text = DirectCast(ot.Item(i), OrdenTrabajo).Id & " - " & (DirectCast(presupuesto.Item(0), Presupuesto).atencion).ToString
                Me.DdlOt.Items.Add(item)
            Next i

        End If


    End Sub

    Public Sub llenarCBPlanificacion()

        Dim plani As ArrayList = CL.ObtenerPlanificacionPrincipal(Me.DdlOt.SelectedValue, 3)
        Me.DdlPlanificacion.Items.Clear()

        If plani.Count > 0 Then
            For i As Integer = 0 To plani.Count - 1
                Dim item As New ListItem
                Dim plan As Planificacion = DirectCast(plani.Item(i), Planificacion)
                item.Value = plan.Id
                Dim d As Date = plan.fecha
                item.Text = plan.Id & " - " & d.ToShortDateString
                Me.DdlPlanificacion.Items.Add(item)
            Next i
        Else
            Me.DdlPlanificacion.Items.Clear()
            Me.DdlMarcaComponente.Items.Clear()

        End If

    End Sub

    Public Sub llenarCBMarcaComponente()

        Dim plani As ArrayList = CL.ObtenerPlanificacionM1(Me.DdlPlanificacion.SelectedValue, 3)
        Me.DdlMarcaComponente.Items.Clear()

        If plani.Count > 0 Then
            For i As Integer = 0 To plani.Count - 1
                Dim item As New ListItem
                Dim plan As Planificacion_M1 = DirectCast(plani.Item(i), Planificacion_M1)
                item.Value = plan.id
                item.Text = plan.marca & " - " & plan.id
                Me.DdlMarcaComponente.Items.Add(item)
            Next i
        Else
            Me.DdlMarcaComponente.Items.Clear()
        End If

    End Sub

#End Region




    Protected Sub DdlOt_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DdlOt.SelectedIndexChanged
        llenarCBPlanificacion()
    End Sub

    Protected Sub DdlPlanificacion_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DdlPlanificacion.SelectedIndexChanged
        llenarCBMarcaComponente()
    End Sub
End Class

